/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.muni.fi.diplomka.datalayer.daoimpl;

import cz.fi.muni.diplomka.common.enumeration.UnitType;
import cz.muni.fi.diplomka.datalayer.dao.CityUnitsDao;
import cz.muni.fi.diplomka.datalayer.entity.CityUnitsEntity;
import cz.muni.fi.diplomka.datalayer.util.DbConnector;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Jakub Kutil
 */
public class CityUnitsDaoImpl implements CityUnitsDao {

    private Connection con = null;
    private PreparedStatement ps = null;
    private ResultSet rs = null;

    public CityUnitsDaoImpl() {
    }

    @Override
    public void addUnitsInCity(CityUnitsEntity unitsInCityEntity) {
        if (unitsInCityEntity == null) {
            throw new IllegalArgumentException("UnitsInCityEntity can not be null");
        }
        try {
            con = DbConnector.getDBConnection();
            String insert = "INSERT INTO city_units"
                    + "(city_id, unit_name, unit_count) VALUES"
                    + "(?,?,?)";
            ps = con.prepareStatement(insert);


            ps.setLong(1, unitsInCityEntity.getCityId());
            ps.setString(2, unitsInCityEntity.getUnit().name());
            ps.setInt(3, unitsInCityEntity.getUnitCount());

            ps.executeUpdate();

            rs = ps.getGeneratedKeys();
            rs.next();


        } catch (SQLException ex) {
            Logger.getLogger(CityUnitsDaoImpl.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            DbConnector.closeConnection(rs, ps, con);
        }
    }

    @Override
    public void editUnitsInCity(CityUnitsEntity unitsInCityEntity) {
        if (unitsInCityEntity == null) {
            throw new IllegalArgumentException("UnitsInCityEntity can not be null");
        }
        try {
            con = DbConnector.getDBConnection();
            String update = "UPDATE city_units SET unit_count=? WHERE city_id=? AND unit_name=?";

            ps = con.prepareStatement(update);

            ps.setInt(1, unitsInCityEntity.getUnitCount());
            ps.setLong(2, unitsInCityEntity.getCityId());
            ps.setString(3, unitsInCityEntity.getUnit().name());

            ps.executeUpdate();

        } catch (SQLException ex) {
            Logger.getLogger(CityUnitsDaoImpl.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            DbConnector.closeConnection(rs, ps, con);
        }
    }

    @Override
    public List<CityUnitsEntity> getAllUnitsInCity(Long playerId) {
        if (playerId == null) {
            throw new IllegalArgumentException("Player ID can not be null");
        }
        List<CityUnitsEntity> unitsInCityEntityList = new ArrayList<CityUnitsEntity>();

        try {
            con = DbConnector.getDBConnection();
            String getAllUnitsInCityOfPlayer = "SELECT * FROM city_units WHERE city_id=?";

            ps = con.prepareStatement(getAllUnitsInCityOfPlayer);
            ps.setLong(1, playerId);

            rs = ps.executeQuery();

            while (rs.next()) {
                CityUnitsEntity unitsInCityEntity = new CityUnitsEntity();
                unitsInCityEntity.setCityId(rs.getLong("city_id"));
                unitsInCityEntity.setUnit(UnitType.valueOf(rs.getString("unit_name")));
                unitsInCityEntity.setUnitCount(rs.getInt("unit_count"));
                unitsInCityEntityList.add(unitsInCityEntity);
            }
        } catch (SQLException ex) {
            Logger.getLogger(CityUnitsDaoImpl.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            DbConnector.closeConnection(rs, ps, con);
        }
        return unitsInCityEntityList;
    }

    @Override
    public int getSpecificUnitCountInCity(Long cityId, UnitType unitType) {
        if (cityId == null) {
            throw new IllegalArgumentException("City ID can not be null");
        }
        if (unitType == null) {
            throw new IllegalArgumentException("Unit Type can not be null");
        }
        CityUnitsEntity unitsInCityEntity = new CityUnitsEntity();

        try {
            con = DbConnector.getDBConnection();
            String getAllSpecificUnitsCountInCityOfPlayer = "SELECT * FROM city_units WHERE city_id=? AND unit_name=?";

            ps = con.prepareStatement(getAllSpecificUnitsCountInCityOfPlayer);
            ps.setLong(1, cityId);
            ps.setString(2, unitType.toString());

            rs = ps.executeQuery();

            while (rs.next()) {
                unitsInCityEntity.setCityId(rs.getLong("city_id"));
                unitsInCityEntity.setUnit(UnitType.valueOf(rs.getString("unit_name")));
                unitsInCityEntity.setUnitCount(rs.getInt("unit_count"));
            }
        } catch (SQLException ex) {
            Logger.getLogger(CityUnitsDaoImpl.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            DbConnector.closeConnection(rs, ps, con);
        }
        return unitsInCityEntity.getUnitCount();
    }
}
